<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer extends CI_Controller {

	public function index(){
	   $this->load->model('Customers', null, true);
       $data['Customers'] = $this->Customers->getList();
	   $this->load->view('customer/index', $data);
	}
    
    /**
     * function add customer
     * @author tien.nguyen
     */
    public function add(){
        $data = array();
        $this->load->model('Customers', null, true);
        $this->load->model('Videos', null, true);
        $this->load->model('CustomerVideos', null, true);
         $this->load->model('Categories', null, true);
        
        //get post
        if($this->input->post()){
            $data = $this->input->post();
            
            //add if
            if($_FILES['Image']['name'] != ''){
                 $resUpload = $this->_uploadFile('Image');
                 if(!is_null($resUpload)){       
                     $data['Image'] = $resUpload['file_name'];    
                     $customerId = $this->Customers->add($data);                    
                 }
            }else{
                $customerId = $this->Customers->add($data);
            }
            
            //add when customer id  <> ''
            if($customerId){
                //insert customer video
                if($data['Videos']){
                    foreach($data['Videos'] as $item){
                        $dataVideo['CustomerId'] = $customerId;
                        $dataVideo['VideoId'] = $item;
                        $this->CustomerVideos->add($dataVideo);
                    }
                }
                
                //redirect to customer,
                edirect(base_url().'customer.html');
            }
            $data['Customer'] = $data;
        }
        $data['OptionsVideos'] = $this->Videos->getOptions();
        $data['CustomerVideos'] = array();
        $data['OptionCategories'] = $this->Categories->getOptions();
        $this->load->view('customer/add', $data);
    }
    
    /**
     * Delete customer
     * @author tien.nguyen
     */
    public function delete($customerId){
         $this->load->model('Customers', null, true);
         if($this->Customers->delete($customerId)){
            redirect(base_url().'customer.html');
         }
    }
    
    /**
     * edit save config id
     * @author tien.nguyen
     */
    public function edit($customerId){
        $res = false;
        $this->load->model('Customers', null, true);
        $this->load->model('Videos', null, true);
        $this->load->model('CustomerVideos', null, true);
         $this->load->model('Categories', null, true);
        
        if($this->input->post()){
            $data = $this->input->post(); 
            
             //add if
            // echo '<pre>'; print_r($_FILES); die;
            if($_FILES['Image']['name'] != ''){
                 $resUpload = $this->_uploadFile('Image');
                 //echo '<pre>'; print_r($resUpload); die;
                 if(!is_null($resUpload)){       
                     $data['Image'] = $resUpload['file_name'];    
                     $customerId = $this->Customers->edit($data, $customerId);                    
                 }
            }else{
                $customerId = $this->Customers->edit($data, $customerId);
            }
            
            //if not exist customer         
            if($customerId){
                //insert customer video
                if($data['Videos']){
                    $this->CustomerVideos->delete($customerId);
                    foreach($data['Videos'] as $item){
                        $dataVideo['CustomerId'] = $customerId;
                        $dataVideo['VideoId'] = $item;
                        $this->CustomerVideos->add($dataVideo);
                    }
                }
                redirect(base_url().'customer.html');          
            }    
        }
        
        $data['Customer'] = $this->Customers->getCustomer($customerId);
        $data['OptionsVideos'] = $this->Videos->getOptions();
        $data['CustomerVideos'] = $this->CustomerVideos->getCustomerVideo($customerId);
        $data['OptionCategories'] = $this->Categories->getOptions();
        $this->load->view('customer/edit', $data);       
    }
    
    /**
     * upload file 
     * @author tien.nguyen
     */
    private function _uploadFile($fileName){
       $config['upload_path'] = $this->config->item('base_upload').'/customers';;
       $config['allowed_types'] = 'gif|png|jpg';
       $config['max_size'] = '1000';
       $config['max_width'] =  '3000';
       $config['max_height'] = '3000';
       
       $this->load->library('upload', $config);
       if(!$this->upload->do_upload($fileName)){
            $this->session->set_flashdata('message', $this->upload->error_msg);
            return null;
       }else{
            return $this->upload->data();
       }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */